var path = require("path"),
    glob = require('glob'),
    webpack = require('webpack'),
    appDir = __dirname + "/app",
    vendorDir = "./bower_components/",
    getEntry = function() {
        var entry = {};
        glob.sync(appDir + '/controllers/*.js').forEach(function(name) {
            var n = name.slice(name.lastIndexOf('controllers/') + 12, name.length - 3).replace('Controller', '');
            entry[n] = name;
        });
        return entry;
    };
var entries = getEntry();
var BowerWebpackPlugin = require("bower-webpack-plugin");


//本地文件
//entries['vendor'] = ['./vendor/template.js', './vendor/knockout-min.js'];
//bower包管理
//entries['vendor'] = [vendorDir + 'art-template/dist/template.js', vendorDir + 'knockout/knockout.js', vendorDir + 'underscore/underscore-min.js', vendorDir + 'jquery/dist/jquery.min.js'];


var config = {
    context: appDir,
    entry: entries,
    //watch:true,
    output: {
        path: path.join(__dirname, "dist"),
        //filename: "[name]-[chunkhash].min.js",
        filename: "[name].min.js",
        chunkFilename: "[id].chunk.js"
    },
    resolve: {
        alias: {
            //本地文件
            // 'knockout': path.join(__dirname, './app/vendor/knockout-min.js'),
            // 'template': path.join(__dirname, './app/vendor/template.js')
            //bower包管理
            // 'jquery': path.resolve(vendorDir, 'jquery/dist/jquery.min.js'),
            // 'knockout': ppath.resolve(vendorDir, 'knockout/knockout.js'),
            // 'underscore': path.resolve(vendorDir, 'underscore/underscore-min.js'),
            // 'template': path.join(vendorDir, 'art-template/dist/template.js')
        }
    },
    module: {
        loaders: [ //加载器
            {
                test: /\.tpl$/,
                loader: "html"
            }, {
                test: /\.hbs$/,
                loader: "handlebars-loader"
            }
            // {
            //     test: path.resolve(vendorDir, 'jquery/dist/jquery.min.js'),
            //     loader: 'expose?jQuery!expose?$'
            // }
        ]
    },
    externals: {
        jquery: 'jQuery',
        underscore: '_',
        knockout: 'ko',
        template: 'template',
        mustache: 'Mustache',
        moment: 'moment',
        handlebars: 'Handlebars'
    },

    // plugins: [
    //     //bower包管理时插件
    //     new BowerWebpackPlugin({
    //         modulesDirectories: ["bower_components"],
    //         manifestFiles: "bower.json",
    //         includes: /.*/,
    //         excludes: [],
    //         searchResolveModulesDirectories: true
    //     }),
    //     new webpack.IgnorePlugin(/\.\/app\/bower_components\/*$/)
    // ]

}


module.exports = config;
//webpack --config webpack.config.js